package com.genius.service.impl;

import com.genius.annotation.PreDataAuth;
import com.genius.mapper.UserMapper;
import com.genius.service.UserService;
import com.genius.system.pojo.User;
import com.genius.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    /**
     * 重置后密码
     */
    private static final String NEW_PASSWORD = "GK123456";

    @Autowired
    private UserMapper userMapper;

    @Override
    public User selectUserByUserName(String username) {
        return userMapper.selectUserByUsername(username);
    }

    @Override
    public boolean selectUsernameIsExist(String username) {
        return userMapper.selectUsernameIsExist(username);
    }

    @Override
    @Transactional
    public int registerUser(User user) {
        return userMapper.registerUser(user);
    }

    @Override
    public User selectUserById(Long userId) {
        return userMapper.selectUserById(userId);
    }

    @Override
    @PreDataAuth(deptAlias = "d", userAlias = "u")
    public List<User> selectUserByPage(User user) {
        return userMapper.selectUserByPage(user);
    }

    @Override
    @Transactional
    public int insertUser(User user) {
        return userMapper.insertUser(user);
    }

    @Override
    @Transactional
    public int updateUser(User user) {
        return userMapper.updateUser(user);
    }

    @Override
    @Transactional
    public int deleteUserByIds(Long[] ids) {
        return userMapper.deleteUserByIds(ids);
    }

    @Override
    @Transactional
    public int updatePassword(User user) {
        return userMapper.updatePassword(user);
    }

    @Override
    @Transactional
    public int updatePasswordByUserIds(Long[] ids) {

        String newPassword = SecurityUtils.encryptPassword(NEW_PASSWORD);
        int rows = 0;
        for(Long userId:ids){
            User user = new User();
            user.setUserId(userId);
            user.setPassword(newPassword);
            rows = rows+userMapper.updatePasswordByUser(user);
        }
        return rows;
    }

}
